<script language="javascript" type="text/javascript">
	function validateForm() {

	var title = document.getElementById("title");
		title.style.backgroundColor="#fff";
		if(title.value == ''){
		alert('Vui lòng nhập tiêu đề!');
		title.focus();
		title.style.backgroundColor="#F90";
		return false;
		}
	var sectionid = document.getElementById("sectionid");
		sectionid.style.backgroundColor="#fff";
		if(sectionid.value == 0){
		alert('Vui lòng chọn chủ đề!');
		sectionid.focus();
		sectionid.style.backgroundColor="#F90";
		return false;
		}	
		
	var catid = document.getElementById("catid");
		catid.style.backgroundColor="#fff";
		if(catid.value == 0){
		alert('Vui lòng chọn danh mục!');
		catid.focus();
		catid.style.backgroundColor="#F90";
		return false;
		}	
	}
</script>
<?php
	$id=$_GET['id'];
	$sql="select * from dm_content where id=".$id;
	$rs=mysql_query($sql);
	$row=mysql_fetch_assoc($rs);

	if(isset($_POST['submit']))
	{
		$title=$_POST['title'];
		if( trim($_POST['alias'])=="")
		{
			$alias= HamhaydungLibs::alias($title);
		}else{
			$alias= preg_replace("/[^a-zA-Z0-9_\-]+/", "", $_POST['alias']);
		}
		$introtext=$_POST['introtext'];
		$fulltext=$_POST['fulltext'];
		$sectionid=$_POST['sectionid'];
		$catid=$_POST['catid'];
		$state=$_POST['state'];		
		$ordering=$_POST['ordering'];
		$metakey=$_POST['metakey'];
		$metadesc=$_POST['metadesc'];
		$created_by=$_POST['created_by'];
		// $created = date('m-d-Y h:i:s', time());
		$created = date('Y-m-d h:i:s', time());
		
		if(isset($id) && $id >0)
		{
			$sql="update dm_content set 
									   title='$title',
									   alias='$alias',
									   introtext='$introtext',
									   `fulltext`='$fulltext',
									   sectionid='$sectionid',
									   catid='$catid',
									   state='$state',
									   ordering='$ordering',
									   metakey='$metakey',
									   metadesc='$metadesc'
										where id=".$id;
			// echo $sql;
			mysql_query($sql);
			echo'<script>alert("Cập nhật bài viết thành công");window.location="?action=content";</script>';
			//update
			
		}else{
			
			$sql="INSERT INTO `dm_content` ( `title`, `alias`, `introtext`, `fulltext`, `state`, `sectionid`, `catid`, `created`, `created_by`, 
											`ordering`, `metakey`, `metadesc` )";
			$sql.= " VALUES	('$title', '$alias', '$introtext', '$fulltext', '$state', '$sectionid', '$catid', 	'$created', '$created_by', 
											'$ordering', '$metakey', '$metadesc' )";
			mysql_query($sql);
			echo'<script>alert("Thêm bài viết thành công");window.location="?action=content";</script>';

		}
		
	}
?>

<!-- Container -->
<div id="container">
	<div class="shell">

		<!-- Main -->
		<div id="main">
			<div class="cl">&nbsp;</div>
			
			<!-- Content -->
			<div id="content">
				<!-- Box -->
				<div class="box">
					<!-- Box Head -->
					<div class="box-head">
						<h2>Thêm bài viết</h2>
					</div>
					<!-- End Box Head -->
					
					<form action="#" id="frmcontent" method="post" enctype="multipart/form-data" onSubmit="return validateForm();">
						
						<!-- Form -->
						<div class="form">
								<p>
									<label>Tiêu đề<span> (*)</span></label>
									<input type="text" name="title" id="title" value="<?php echo $row['title'] ?>" class="field size1" />
								</p>	
										
								<p>
									<label>Alias</label>
									<input type="text" name="alias" value="<?php echo $row['alias'] ?>" class="field size1" />
								</p>

								<p>
									<label>Mô tả ngắn</label>
									<textarea id="introtext" name="introtext" class="field size1" rows="7" cols="30"><?php echo $row['introtext'] ?></textarea>
								</p>	
							
								<p>
									<label>Nội dung</label>
									<textarea id="fulltext" name="fulltext" class="field size1" rows="10" cols="30"><?php echo $row['fulltext'] ?></textarea>
								</p>	
															

						</div>
										
				</div>
				<!-- End Box -->

			</div>
			<!-- End Content -->
			
			<!-- Sidebar -->
			<div id="sidebar">
				
				<!-- Box -->
				<div class="box">
					
					<!-- Box Head -->
					<div class="box-head">
						<h2>Chức năng</h2>
					</div>
					<!-- End Box Head-->

					<div class="box-content">
						<div class="buttons">
							<input type="button" onclick="window.location.href='?action=content'" class="button" value="Thoát" />
							<!--<input type="button" onclick="validateForm()" name="submit" class="button" value="Lưu" />-->
							<input type="submit"  name="submit" class="button" value="Lưu" />
						</div>
	
						
						<!-- Sort -->
						<div class="sort">								

							<label>Chủ đề</label>						
							<select name="sectionid" id="sectionid" class="field" onchange="getCategoryBySection(this.value)">
								<option value="0">Tất cả chủ đề</option>
							
								<?php
								$sqlsec="select id, title from dm_sections where published=1";								
								$querysec=mysql_query($sqlsec);	
								while($rowsec=mysql_fetch_array($querysec))
								{
									if($row['sectionid']==$rowsec['id'])
									{
										$selected = "SELECTED";
									}else{
										$selected = "";
									}
									echo "<option $selected value='$rowsec[id]'>$rowsec[title]</option>";
								}			
								?>
							</select>
							
							<label>Danh mục</label>		

							<?php
							if(isset($row['catid']))
							{
								echo '<select name="catid" id="catid" class="field">';
								$sqlcat="select id, title from dm_categories where published=1";								
								$querycat=mysql_query($sqlcat);	
								while($rowcat=mysql_fetch_array($querycat))
								{
									if($row['catid']==$rowcat['id'])
									{
										$selected = "SELECTED";
									}else{
										$selected = "";
									}
									echo "<option $selected value='$rowcat[id]'>$rowcat[title]</option>";
								}
								echo "</select>";
							}else{
							?>	
							<select name="catid" id="catid" class="field">
								<option value="0">Tất cả danh mục</option>
							</select>
							<?php
							}
							?>
							
							<div style="margin:16px;  background-color: gainsboro; height: 17px;">
							<?php
							if(!isset($row['state']))
							{
								$checked0 = "";
								$checked1 = "CHECKED";
							}else{
								if($row['state']==1)
								{
									$checked0 = "";
									$checked1 = "CHECKED";
								}else{
									$checked0 = "CHECKED";
									$checked1 = "";
								}
							}
							?>
							<span style="margin-right:10px; margin-left: 20px;">Bật</span><INPUT TYPE=radio <?php echo $checked1?>  NAME="state" VALUE="1">&nbsp &nbsp &nbsp &nbsp &nbsp
							<span style="margin-right:10px">Tắt</span><INPUT TYPE=radio <?php echo $checked0?> NAME="state" VALUE="0">
							</div>	
								
							
							<label>Thứ tự</label>
							<input type="text" name="ordering" value="<?php echo $row['ordering'] ?>" class="field" />
							
							<label>Keywords</label>
							<textarea cols="30" rows="10" class="field" name="metakey" id="metakey" style="width: 188px; height: 60px;"><?php echo $row['metakey'] ?></textarea>
						
							<label>description</label>
							<textarea id="metadesc" name="metadesc" class="field" rows="10" cols="30" style="width: 188px; height: 175px;"><?php echo $row['metadesc'] ?></textarea>
								
							<input type="hidden" value="<?php echo $id?>" name="id" />
							<input type="hidden" value="1" name="created_by" />
										
						</div>
						<!-- End Sort -->
						
					</div>
				</div>
				<!-- End Box -->
			</div>
			<!-- End Sidebar -->
			</form>
			<div class="cl">&nbsp;</div>			
		</div>
		<!-- Main -->
	</div>
</div>
<!-- End Container -->
<script>
	var editor=CKEDITOR.replace( 'fulltext' );
	CKFinder.setupCKEditor( editor, '../libs/ckfinder/' ) ;


</script>
